/**
 * JAVA服务器字牌
 * Created by duwei on 2019/3/21.
 */
var ZiPaiJavaRoom = ZiPaiRoom.extend({
    opType: {
        OP_CHI: 1,
        OP_PENG: 2,
        OP_PAO: 3,
        OP_WEI: 4,
        OP_TI: 5,
        OP_HU: 6,
        OP_PASS: 7,
        OP_CHOUWEI : 8,
        OP_CHONGPAO : 9,
        OP_WANGDIAO:21,
        OP_WANGCHUANG:22,
        OP_WANGZHA:23
    },
    afterGameStart: null,
    /**
     * 吃碰后保留自己的摆牌
     */
    chiPengCardDeal: false,
    /**
     * 设置管理器
     * @param data
     * @param roomDataType
     */
    setZipaiManager: function (data, roomDataType) {
        this.zipaiManager = new ZiPaiJavaManager(data, roomDataType, this);
        this.addComponent(this.zipaiManager);
    },
    /**
     * 构造
     * @param data
     * @param roomDataType
     * @returns {boolean}
     */
    ctor: function (data, roomDataType) {
        this._super(data, roomDataType);
        if (!cc.sys.isNative) {
            //this.initTestFapai();
        }
        return true;
    },
    /**
     * 设置庄家
     */
    setZhuang: function () {
        var self = this;
        for (var i = 0; i < this.zipaiManager.PlayerInfos.length; i++) {
            var uid = this.zipaiManager.PlayerInfos[i]['uid'];
            var row = this.zipaiManager.g_uid_to_pos[uid];
            var infoNode = self.$('info' + row, self.PlayerInfoNode);
            if (infoNode) {
                infoNode.getChildByName('zhuang').setVisible(uid == this.zipaiManager.BankerUserID);
            }
        }
    },
    initTestFapai: function () {
        var that = this;
        var test = new cc.Sprite('res/icon.png');
        test.setPosition(cc.p(200, 700));
        that.addChild(test);
        TouchUtil.setOnclickListener(test, function () {
            that.addChild(new TestZiPai());
        });
    },
    onCCSLoadFinish: function () {
        this._super();
        this.setPlayersStatus();
        this.setDisconnect();
    },
    /**
     * 设置离线状态
     */
    setDisconnect: function () {
        var self = this;
        for (var i = 0; i < this.zipaiManager.PlayerInfos.length; i++) {
            var uid = this.zipaiManager.PlayerInfos[i]['uid'];
            var row = this.zipaiManager.g_uid_to_pos[uid];
            var offline = this.zipaiManager.g_uid_to_offline[uid];
            var infoNode = self.$('info' + row, self.PlayerInfoNode);
            if (infoNode) {
                var uiOffline = infoNode.getChildByName('offline');
                var time = uiOffline.getChildByName('time');
                if (offline && !uiOffline.isVisible()) {
                    time.timeNum = -1;
                    self.setOfflineTime(time);
                }
                uiOffline.setVisible(offline);
            }
        }
    },
    setRoomState: function () {
        var self = this;
        var isCreatedRoom = self.zipaiManager.GameStatus == self.zipaiManager.roomStatus.CREATED;
        self.$('FunctionBtn.bottomCenter.btn_invite').setVisible(isCreatedRoom && !self.zipaiManager.isReplay);
        //self.$('FunctionBtn.btn_copy').setVisible(isCreatedRoom && !self.zipaiManager.isReplay);
        //self.$('FunctionBtn.bottomCenter.img_dengdaijiaru').setVisible(isCreatedRoom && !self.zipaiManager.isReplay);
        //self.$('FunctionBtn.bottomCenter.btn_tuichu').setVisible(isCreatedRoom && self.zipaiManager.Owner != gameData.uid && !self.zipaiManager.isReplay);
        //self.$('FunctionBtn.bottomCenter.btn_jiesan').setVisible(isCreatedRoom && self.zipaiManager.Owner == gameData.uid && !self.zipaiManager.isReplay);



        self.$('guding.btn_Setting').setVisible(!self.zipaiManager.isReplay);
        self.$('guding.btn_shuaxin').setVisible(!self.zipaiManager.isReplay);
        self.$('guding.btn_dingwei').setVisible(!self.zipaiManager.isReplay);
        self.$('guding.btn_likai').setVisible(!self.zipaiManager.isReplay);
        self.$('FunctionBtn.btn_chat').setVisible(true);
        self.$('FunctionBtn.btn_mic').setVisible(true);
    },
    initTouch: function () {
        this._super();
        var self = this;
        /**
         * 点击准备按钮
         */
        TouchUtil.setOnclickListener(self.$('FunctionBtn.bottomCenter.btn_zhunbei'), function (node) {
            network.send(3004, {room_id: gameData.roomId});
            node.setVisible(false);
        });
        //快速开局
        TouchUtil.setOnclickListener(self.$('FunctionBtn.btn_quick'), function (node) {
            //network.sendPhz(5000, 'QuickStart/3/' + self.getQuickStartOption());
            node.setVisible(false);
        });

        //退出
        TouchUtil.setOnclickListener(self.$('FunctionBtn.bottomCenter.btn_tuichu'), function () {
            //不判别是否为房主
            //if (gameData.uid != gameData.ownerUid) {
                showMessageBox('确定要退出房间吗?', function () {
                    network.send(3003, {room_id: gameData.roomId});
                });
            //}
        });
        //解散
        TouchUtil.setOnclickListener(self.$('FunctionBtn.bottomCenter.btn_jiesan'), function () {
            if (window.inReview)
                network.send(3003, {room_id: gameData.roomId});
            else
                showMessageBox('解散房间不扣房卡，是否确定解散？', function () {
                    network.send(3003, {room_id: gameData.roomId});
                });
        });
        //离开
        TouchUtil.setOnclickListener(self.$('guding.btn_likai'), function () {
            // 申请投票解散
            var fun1 = function () {
                if (window.inReview)
                    network.send(3009, {room_id: gameData.roomId, is_accept: 1});
                else
                    showMessageBox('确定要申请解散房间吗？', function () {
                        network.send(3009, {room_id: gameData.roomId, is_accept: 1});
                    });
            };
            //房主解散房间
            var fun2 = function () {
                if (window.inReview)
                    network.send(3003, {room_id: gameData.roomId});
                else
                    showMessageBox('解散房间不扣房卡，是否确定解散？', function () {
                        network.send(3003, {room_id: gameData.roomId});
                    });
            };
            //其他玩家退出房间
            var func3 = function () {
                showMessageBox('确定要退出房间吗?', function () {
                    network.send(3003, {room_id: gameData.roomId});
                });
            };
            if (self.zipaiManager.isGameStart) {
                fun1();
            } else {
                if (self.zipaiManager.Owner == gameData.uid) {
                    fun2();
                } else {
                    func3();
                }
            }

        });
    },
    initEvent: function () {
        this._super();
        var self = this;
        eventManager.addListener(ZiPaiEvent.onTurnRow, this.onTurnRow.bind(this), this);
        eventManager.addListener(ZiPaiEvent.onMoCard, this.onMoCard.bind(this), this);
        eventManager.addListener(ZiPaiEvent.onChuCard, this.onChuCard.bind(this), this);
        eventManager.addListener(ZiPaiEvent.onCardAscription, this.onCardAscription.bind(this), this);
        eventManager.addListener(ZiPaiEvent.onChiPengGangHu, this.onChiPengGangHu.bind(this), this);
        eventManager.addListener(ZiPaiEvent.showChiPengGangHu, this.showChiPengGangHu.bind(this), this);
        eventManager.addListener(ZiPaiEvent.showGameResult, this.showGameResult.bind(this), this);
        eventManager.addListener(ZiPaiEvent.showSelectPiao, this.onSelectedPiao.bind(this), this);
        eventManager.addListener(ZiPaiEvent.onSelectedPiao, this.onSelectedPiao.bind(this), this);
        eventManager.addListener(ZiPaiEvent.onShowSishou, this.showSishouResult.bind(this), this);
    },
    setPiaoTouch: function () {
        var self = this;
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao0'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 0});
        });
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao2'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 2});
        });
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao5'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 5});
        });
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao10'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 10});
        });
    },
    /**
     * 选票
     * @param data
     */
    onSelectedPiao: function (data) {
        var self = this;
        var selected = data['selected'] || [];
        var isAllSelected = data['is_finished'];
        if (isAllSelected) {
            for (var i = 0; i < selected.length; i++) {
                var o = selected[i];
                var row = self.zipaiManager.g_uid_to_pos[o.uid];
                var infoNode = self.$('info' + row, self.PlayerInfoNode);
                var piao = infoNode.getChildByName('piao');
                if (o.piao > 0) {
                    piao.setString('飘' + o.piao);
                } else {
                    piao.setString('飘' + 0);
                }
                piao.setVisible(true);
            }
            self.$('PiaoNode').setVisible(false);
        } else {
            for (var i = 0; i < selected.length; i++) {
                var o = selected[i];
                var row = self.zipaiManager.g_uid_to_pos[o.uid];
                var infoNode = self.$('info' + row, self.PlayerInfoNode);
                var piao = infoNode.getChildByName('piao');
                if (o.uid == gameData.uid) {
                    self.$('PiaoNode').setVisible(o.piao < 0);
                    self.setPiaoTouch();
                    //continue;
                }
                if (o.piao < 0) {
                    piao.setString('选飘中');
                } else {
                    piao.setString('飘' + o.piao);
                }
                piao.setVisible(true);
                piao.ignoreContentAdaptWithSize(true);
            }
        }
    },
    addPlayerBackWindow: function () {
        var self = this;
        if (self.zipaiManager.isReplay) {
            showWindow('Zipai_PlayerBackJavaWindow', self.zipaiManager.roomReplayDate, this);
        }
    },
    networkStop: function () {
        network.stop();
    },
    setReplayProgress: function (cur, total) {
        var that = this;
        var progress = cur / total * 100;
        //this.showTip("回放进度: " + progress.toFixed(1) + '%', false);
    },
    setAfterGameStart: function (cb) {
        this.afterGameStart = cb;
    },
    setAllPai4Replay: function (data) {
        data = deepCopy(data);
        mAction.initGame();
        for (var uid in data)
            if (data.hasOwnProperty(uid)) {
                var playercard = data[uid];
                var pos = this.zipaiManager.g_uid_to_pos[uid];
                if(cc.isUndefined(pos)){
                    mAction.leftCardCount = data['uid'];
                    this.zipaiManager.setLeftCardCount(mAction.leftCardCount);
                    this.setLeftCard();
                    this.hideReplayAction();
                    continue;
                }
                var outCards = this.getOutNode(pos);//room['out' + pos];
                var openCards = this.getOpenNode(pos);//room['open' + pos];
                if (pos == 0) {
                    this.zipaiManager.setSeverCardList(playercard['pai_arr']);
                    this.zipaiManager.setCardList();
                    this.setUpCards();
                } else {
                    this.zipaiManager.setOtherCardList(playercard['pai_arr'], pos);
                    this.setUpOtherCards(pos);
                }
                // usedMoPaiArr
                // usedChuPaiArr
                // for (var j = 0; j < playercard['dui_arr'].length; j++) {
                //     var card = playercard['dui_arr'][j];
                //     outCards.addCard(card);
                // }
                // for (j = 0; j < playercard.open.length; j++) {
                //     openCards.addCards(playercard.open[j].cards, playercard.open[j].typ, false);
                // }

                outCards.removeAllChildren();
                for (var j = 0; j < playercard['used_arr'].length; j++) {
                    var card = playercard['used_arr'][j];
                    outCards.addCard(card);
                }

                openCards.removeAllChildren();
                for (j = 0; j < playercard['dui_arr'].length; j++) {
                    var cards =  playercard['dui_arr'][j]['pai_arr'];
                    var typ = mCard.getComboType(cards, false);
                    openCards.addCards(cards, typ);
                }
                //设置胡息
                this.setUpHX(pos);
            }
    },
    setReplayChiPengBtn: function (row, op) {
        var self = this;
        var replay_act = self.$('replay_act' + row, self.PlayerInfoNode);
        var opStr = 'guo';
        if (!!replay_act) {
            switch (op) {
                case self.opType.OP_CHI:
                    opStr = 'chi';
                    break;
                case self.opType.OP_PENG:
                    opStr = 'peng';
                    break;
                case self.opType.OP_HU:
                    opStr = 'hu';
                    break;
                case self.opType.OP_PASS:
                    opStr = 'guo';
                    break
            }
            var bt = self.$(opStr, replay_act);
            if (bt) {
                self.playHand(bt);
            }
        }
    },
    throwTurnByUid: function (uid) {
        this.onTurnRow({uid: uid});
    },
    /**
     * 设置玩家准备状态
     */
    setPlayersStatus: function () {
        var self = this;
        var btn_zhunbei = self.$('FunctionBtn.bottomCenter.btn_zhunbei');
        btn_zhunbei.setVisible(this.zipaiManager.isShowReadyBtn && !this.panelResult.isVisible());
        for (var i = 0; i < this.zipaiManager.PlayerInfos.length; i++) {
            var uid = this.zipaiManager.g_pos_to_uid[i];
            self.setReady(uid, this.zipaiManager.g_uid_to_ready[uid]);
        }
    },
    /**
     * 游戏准备
     */
    onReady: function () {
        this.clearRoom();
        network.send(3004, {room_id: gameData.roomId});
        var players = this.zipaiManager.PlayerInfos;
        for (var i = 0; i < players.length; i++) {
            // this.setJushou(players[i].ID, 0, 0, false);//举手
            // this.setBaojing(players[i].ID, 0);//报警
        }
    },
    /**
     * 发牌
     * 4200
     */
    setUpCards: function (isReplay) {
        this._super();
        if (this.afterGameStart && isReplay)
            this.afterGameStart();
    },
    /**
     * 操作人
     * 4000
     * @param data
     */
    onTurnRow: function (data) {
        var self = this;
        var uid = data['uid'];
        var row = self.zipaiManager.g_uid_to_pos[uid];
        this.setTurnInOver();
        this.showCard.setVisible(false);
        this.turnPos = row;
        this.secondsLeft = 15;
        this.setTurnOutArrow();
        if (this.turnPos == 0) {
            this.isTurnOut = true;
            this.isTurnIn = false;
        }
    },
    moCard: function () {
        cc.log('moCard ==' + this.zipaiManager.LeftCardCount);
        this.game_zhang.setVisible(this.zipaiManager.LeftCardCount >= 0);
        this.txtCardCount.setString(this.zipaiManager.LeftCardCount);
    },
    /**
     * 摸牌
     * 4001
     * @param data
     */
    onMoCard: function (data) {
        var self = this;
        this.showCDTime(-1);
        this.networkStop();
        this.scheduleOnce(function () {
            network.start();
        }, 0.55);
        var uid = data['uid'];
        var pai_id = data['pai_id'];
        var row = self.zipaiManager.g_uid_to_pos[uid];
        var isWangfan = data['wangFan'] == 1 ? true : false;
        self.showCardAtPos(pai_id, row, true, data['liangZhang'], isWangfan);
        self.moCard();
    },
    /**
     * 出牌
     * 4002
     * @param data
     */
    onChuCard: function (data) {
        this.networkStop();
        this.scheduleOnce(function () {
            network.start();
        }, 0.5);
        //{"code":4002,"data":{"room_id":218229,"uid":202,"pai_id":7,"pai_arr":[5,14,8,8,2,2,20,10,19,13,15,6,6,16,9,4,14,11,18,5],"idx":0},"errorCode":0}
        var self = this;
        var uid = data['uid'];
        var pai_id = data['pai_id'];
        var row = self.zipaiManager.g_uid_to_pos[uid];
        self.setSameCardShow(pai_id, false);
        self.showCardAtPos(pai_id, row, false);
        if (row == 0 || this.zipaiManager.isReplay) {
            var deleteCards = [pai_id];
            self.removeCards(deleteCards, row, self.outCardColumn, self.outCardRow);
            self.outCardColumn = null;
            self.outCardRow = null;
        }
        if (pai_id == 0) {
            self.playEffectByMap(pai_id.toString(), gameData.sex);
        }
        if (self.zipaiManager.isReplay) {
            this.hideReplayAction();
        }
    },
    /**
     * 牌归属
     * 4006
     * @param data
     */
    onCardAscription: function (data) {
        var self = this;
        //{"code":4006,"data":{"room_id":155648,"paiId":9,"uid":198,"fromUid":198},"errorCode":0}
        var uid = data['uid'];
        var paiId = data['paiId'];
        var fromUid = data['fromUid'];
        //var isShowCard = false;
        if (uid == 0) {//弃牌
            var userPos = self.zipaiManager.g_uid_to_pos[fromUid];
            var out = this.getOutNode(userPos);
            //isShowCard = true;
            out.addCard(paiId, true, this.showCard);
        } else {
            var row = self.zipaiManager.g_uid_to_pos[uid];
            if (row == 0) {
                if (data['seq'] != 1 && paiId == 21) {
                    this.zipaiManager.addCard21();
                    if (this.movecard && this.positionBak && (this.isBeginTouch)) {
                        this.moveCardBack();
                    } else {
                        this.setUpCards();
                    }

                }
                else {
                    this.zipaiManager.addCard(paiId, null, this.showCard);
                    if (this.movecard && this.positionBak && (this.isBeginTouch)) {
                        this.moveCardBack();
                    } else {
                        this.setUpCards();
                    }
                }

            }
            else {
                if (self.zipaiManager.isReplay == true && row > 0 && paiId == 21) {
                    self.zipaiManager.setOtherCardListBy21(row);
                    eventManager.dispatchEvent(ZiPaiEvent.UpOtherCards, row);
                }
            }
        }
        //if (isShowCard == false) {
            this.showCard.setVisible(false);
        //}
        if (self.zipaiManager.isReplay) {
            this.hideReplayAction();
        }
    },
    // /**
    //  * 显示吃碰杠胡动画
    //  * 4003
    //  * @param data
    //  */
    // showChiPengGangHu: function (data) {
    //     var self = this;
    //     var uid = data['uid'];
    //     var pai_id = data['pai_id'];
    //     var from_uid = data['from_uid'];
    //     var row = self.zipaiManager.g_uid_to_pos[uid];
    //     var op = data['op'];
    //     var open = self.getOpenNode(row);
    //     switch (op) {
    //         case self.opType.OP_CHI:
    //             var chiPaiArr = data['chiPaiArr'];
    //             var count = chiPaiArr.length;
    //             for (var i = 0; i < count; i++) {
    //                 var combo = chiPaiArr[i];
    //                 var comboTyp = mCard.getComboType(combo);
    //                 open.addCards(combo, comboTyp, false);
    //             }
    //             mEffect.chiPai(open, row);
    //             self.showActAnimation(mCard.comboTypes.chi, row);
    //             if (count > 1) {
    //                 self.playCPTPHEffect('bi', gameData.sex);
    //             } else {
    //                 self.playCPTPHEffect('chi', gameData.sex);
    //             }
    //             break;
    //         case self.opType.OP_PENG:
    //             open.addCards([pai_id, pai_id, pai_id], mCard.comboTypes.peng);
    //             mEffect.chiPai(open, row);
    //             self.showActAnimation(mCard.comboTypes.peng, row);
    //             self.playCPTPHEffect('peng', gameData.sex);
    //             break;
    //         case self.opType.OP_HU:
    //             this.showActAnimation(mCard.comboTypes.hu, row);
    //             break;
    //         case self.opType.OP_TI:
    //             var isOpen = false;
    //             //info == true牌桌上的偎
    //             // if (broadCastData.info == 'true') {
    //             //     isOpen = true;
    //             // }
    //             open.addCards([pai_id, pai_id, pai_id, pai_id], mCard.comboTypes.ti, isOpen);
    //             mEffect.chiPai(open, row);
    //             this.playCPTPHEffect('ti', gameData.sex);
    //             break;
    //         case self.opType.OP_PAO:
    //             var isOpen = false;
    //             //info == true牌桌上的偎
    //             // if (broadCastData.info == 'true') {
    //             //     isOpen = true;
    //             // }
    //             open.addCards([pai_id, pai_id, pai_id, pai_id], mCard.comboTypes.pao, isOpen);
    //             mEffect.chiPai(open, row);
    //             this.playCPTPHEffect('pao', gameData.sex);
    //             break;
    //         case self.opType.OP_WEI:
    //             open.addCards([pai_id, pai_id, pai_id], mCard.comboTypes.wei);
    //             mEffect.chiPai(open, row);
    //             this.playCPTPHEffect('wei', gameData.sex);
    //             break;
    //
    //     }
    //     if(this.zipaiManager.isReplay){
    //         self.setReplayChiPengBtn(row,op);
    //     }
    //     self.showCard.setVisible(false);
    //     self.setUpHX(row);
    // },
    /**
     * 显示吃碰杠胡动画
     * 4003
     * @param data
     */
    showChiPengGangHu: function (data) {
        var self = this;
        var uid = data['uid'];
        var pai_id = data['pai_id'];
        var from_uid = data['from_uid'];
        var row = self.zipaiManager.g_uid_to_pos[uid];
        var op = data['op'];
        //存储亮牌区，用于小结显示桌面(胡会清空dui_arr)
        if (op != 6) self.zipaiManager.g_uid_to_opencards[uid] = deepCopy(data['dui_arr']) || [];
        
        var arrangeOpencards = function (isNotDelay) {
            var open = self.getOpenNode(row);
            if (isNotDelay) {
                if (data['dui_arr']) {
                    open.setPos(row);
                    for (var j = 0; j < data['dui_arr'].length; j++) {
                        var menzi = data['dui_arr'][j];
                        var cards = menzi['pai_arr'];
                        var typ = mCard.getJavaComboType(menzi['type']);
                        open.addCards(cards, typ, false, menzi['chiPaiId']);
                    }
                }
            }
            else {
                open.runAction(cc.sequence(
                    cc.delayTime(0.55),
                    cc.callFunc(function () {
                        // child3.setPositionY(child3.getPositionY() - 75);
                        if (data['dui_arr']) {
                            open.setPos(row);
                            for (var j = 0; j < data['dui_arr'].length; j++) {
                                var menzi = data['dui_arr'][j];
                                var cards = menzi['pai_arr'];
                                var typ = mCard.getJavaComboType(menzi['type']);
                                open.addCards(cards, typ, false, menzi['chiPaiId']);
                            }
                        }
                    })
                ));
            }
        };
        arrangeOpencards(true);
        
        //臭啸
        if (op == self.opType.OP_WEI && data['op2'] && data['op2'] > 1) op = self.opType.OP_CHOUWEI;
        //重跑
        if (op == self.opType.OP_PAO && data['op2']&& data['op2'] > 1) op = self.opType.OP_CHONGPAO;
        //王番
        if (op == self.opType.OP_HU) {
            if (data['wangFan'] == 21) op = self.opType.OP_WANGDIAO;
            if (data['wangFan'] == 22) op = self.opType.OP_WANGCHUANG;
            if (data['wangFan'] == 23) op = self.opType.OP_WANGZHA;
        }

        switch (op) {
            case self.opType.OP_CHI:
                var chiPaiArr = data['chiPaiArr'];
                var count = chiPaiArr.length;
                if (count > 1) {
                    self.showActAnimation(mCard.comboTypes.xiahuo, row);
                    self.playCPTPHEffect('bi', gameData.sex);
                } else {
                    self.showActAnimation(mCard.comboTypes.chi, row);
                    self.playCPTPHEffect('chi', gameData.sex);
                }
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_PENG:
                self.showActAnimation(mCard.comboTypes.peng, row);
                self.playCPTPHEffect('peng', gameData.sex);
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_HU:
                self.showActAnimation(mCard.comboTypes.hu, row);
                self.playCPTPHEffect('hu', gameData.sex);
                break;
            case self.opType.OP_TI:
                self.showActAnimation(mCard.comboTypes.ti, row);
                self.playCPTPHEffect('ti', gameData.sex);
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_PAO:
                self.showActAnimation(mCard.comboTypes.pao, row);
                self.playCPTPHEffect('pao', gameData.sex);
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_CHONGPAO:
                self.showActAnimation(mCard.comboTypes.chongpao, row);
                self.playCPTPHEffect('pao', gameData.sex);
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_WEI:
                self.showActAnimation(mCard.comboTypes.wei, row);
                self.playCPTPHEffect('wei', gameData.sex);
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_CHOUWEI:
                self.showActAnimation(mCard.comboTypes.chouwei, row);
                self.playCPTPHEffect('wei', gameData.sex);
                var opencards = self.getOpenNode(row);
                mEffect.chiPai(opencards, row, false, data);
                arrangeOpencards();
                break;
            case self.opType.OP_WANGDIAO:
                self.showActAnimation(mCard.comboTypes.wangdiao, row);
                self.wangCallBack(1, row);
                self.playCPTPHEffect('wangdiao', gameData.sex);
                if (pai_id != 0) {
                    setTimeout(function () {
                        if (self) {
                            self.showActAnimation(mCard.comboTypes.hu, row);
                        }
                    }, 1300);
                    this.scheduleOnce(function () {
                        self.playEffectByMap(pai_id, gameData.sex);
                    }, 0.8);
                    this.scheduleOnce(function () {
                        self.playCPTPHEffect('hu', gameData.sex);
                    }, 1.6);
                }
                break;
            case self.opType.OP_WANGCHUANG:
                self.showActAnimation(mCard.comboTypes.wangchuang, row);
                self.wangCallBack(2, row);
                self.playCPTPHEffect('wangchuang', gameData.sex);
                if (pai_id != 0) {
                    setTimeout(function () {
                        if (self) {
                            self.showActAnimation(mCard.comboTypes.hu, row);
                        }
                    }, 1300);
                    this.scheduleOnce(function () {
                        self.playEffectByMap(pai_id, gameData.sex);
                    }, 0.8);
                    this.scheduleOnce(function () {
                        self.playCPTPHEffect('hu', gameData.sex);
                    }, 1.6);
                }
                break;
            case self.opType.OP_WANGZHA:
                self.showActAnimation(mCard.comboTypes.wangzha, row);
                self.wangCallBack(3, row);
                self.playCPTPHEffect('wangzha', gameData.sex);
                if (pai_id != 0) {
                    setTimeout(function () {
                        if (self) {
                            self.showActAnimation(mCard.comboTypes.hu, row);
                        }
                    }, 1300);
                    this.scheduleOnce(function () {
                        self.playEffectByMap(pai_id, gameData.sex);
                    }, 0.8);
                    this.scheduleOnce(function () {
                        self.playCPTPHEffect('hu', gameData.sex);
                    }, 1.6);
                }
                break;
        }
        if (this.zipaiManager.isReplay) {
            self.setReplayChiPengBtn(row, op);
        }
        self.isHuOtherPerson = false;
        self.isSelfHuOtherPerson = false;
        self.isWangfanHu = false;
        self.wangfanType = 0;
        if (op == self.opType.OP_HU || op == self.opType.OP_WANGDIAO || op == self.opType.OP_WANGCHUANG || op == self.opType.OP_WANGZHA) {
            if (op == self.opType.OP_WANGDIAO || op == self.opType.OP_WANGCHUANG || op == self.opType.OP_WANGZHA) {
                self.isWangfanHu = true;
                self.wangfanType = op;
            }
            if (gameData.uid == uid){
                self.isHuOtherPerson = false;
                self.showCard.setVisible(false);
            }
            else {
                self.isHuOtherPerson = true;
                var delayt = self.isWangfanHu ? 0.8 : 0.5;
                self.showCard.runAction(cc.sequence(
                    cc.delayTime(delayt),
                    cc.callFunc(function () {
                        self.showCard.setVisible(true);
                    })
                ));
            }

            if (pai_id == 21) {
                self.showCard.setVisible(false);
            }
            if (gameData.uid == uid && uid != from_uid){
                self.isSelfHuOtherPerson = true;
            }

        }
        else{
            self.showCard.setVisible(false);
        }
        if(gameData.wanfatype == 'java' &&  data['huxi']){
            mAction.hx[row] = data['huxi'];
            mAction.showHx[row] =data['huxi'];
        }
        self.setUpHX(row);
    },
    wangCallBack:function (wangcount,row) {
        var self = this ;
        var deleteCards = [];
        if(wangcount){
            for(var i = 0 ; i < wangcount ;i++ ){
                deleteCards.push(21);
            }
        }
        if(row == 0){
            self.removeCards(deleteCards, row);
        }
        var cardsNode = self.$('PlayerInfo.node_wang');
        if(cardsNode){
            cardsNode.removeAllChildren();
        }
        var PlayerInfo = self.$('PlayerInfo');
        cardsNode.setPosition(cc.p(cc.winSize.width/2,cc.winSize.height/2+300));
        //cardsNode.setScale(0);
        cardsNode.setLocalZOrder(100);
        var card21Arr = [];
        for (var j = 0; j < deleteCards .length; j++) {
            var card = new Card();
            cardsNode.addChild(card);
            card.setData(deleteCards[j], null, 1);
            card.setLocalZOrder(j);
            card.setScale(1);
            card.setPosition(cc.p(30*j,0));
            card21Arr.push(card);
        }
        var pos =  self.$("nCards"+row , self.PosNode);
        if(row==0){
            pos = self.$('info' + row, self.PlayerInfoNode);
        }
        var orderPos = pos.getParent().convertToWorldSpace(pos.getPosition());
        if(row == 0){
            orderPos.x += 140;
            orderPos.y += 60;
        }
        else {
            if (app.map_id.QYLDS == gameData.mapId || app.map_id.YZCHZ == gameData.mapId){
                if (this.zipaiManager.PlayersNum == 4) {
                    if (row == 1 || row ==2 ) {
                        orderPos.x += 150;
                    }
                }
                if (this.zipaiManager.PlayersNum == 3) {
                    if (row == 1) {
                        orderPos.x += 150;
                    }
                }
            }
        }
        var cardsPosOffset = cc.p(orderPos.x - cardsNode.getPosition().x, orderPos.y - cardsNode.getPosition().y);

        var tmpCard21ArrLen = card21Arr.length;
        var tmpLocation = 0;
        var interval;
        var intervalFunc = function () {
            if (!self || tmpLocation == tmpCard21ArrLen)
                return clearInterval(interval);
            if (card21Arr[tmpLocation]){
                card21Arr[tmpLocation].setVisible(true);
                card21Arr[tmpLocation].setPosition(cc.p(0, 0));
                card21Arr[tmpLocation].runAction(
                    cc.spawn(
                        cc.moveTo(0.3,cc.p(cardsPosOffset.x + 30 * tmpLocation, cardsPosOffset.y)).easing(cc.easeOut(2.0)),
                        cc.scaleTo(0.4,1).easing(cc.easeOut(2.0))
                    )
                );

                tmpLocation ++;
            }
            else {
                return clearInterval(interval);
            }
        };
        interval = setInterval(intervalFunc, 100);

        //cardsNode.runAction(cc.spawn(cc.moveTo(0.2,orderPos).easing(cc.easeOut(2.0)),cc.scaleTo(0.25,1).easing(cc.easeOut(2.0))))

    },
    /**
     * 显示吃碰杠胡按钮
     * 4005
     * @param data
     */
    onChiPengGangHu: function (data) {
        var self = this;
        self.isTurnOut = false;
        self.isTurnIn = true;
        self.secondsLeft = 15;
        var option_type = {
            0: self.opType.OP_CHI,
            1: self.opType.OP_PENG,
            2: self.opType.OP_HU,
            3: self.opType.OP_PASS
        };
        var op_map = {};
        for (var key in self.opType) {
            if (self.opType.hasOwnProperty(key)) {
                op_map[self.opType[key]] = [];
            }
        }
        op_map[self.opType.OP_PASS].push(0);
        var tishi = data['tishi'];
        for (var index = 0; index < tishi.length; index++) {
            var pai_id = tishi[index]['pai_id'];
            var op = tishi[index]['op'] || [false, false, false];
            for (var op_idx = 0; op_idx < op.length; op_idx++) {
                if (op[op_idx]) {
                    op_map[option_type[op_idx]].push(pai_id);
                }
            }
        }
        self.showActions(data, op_map);
    },
    /**
     * 显示吃、碰、过、胡 按钮
     * @param data
     * @param op_map
     */
    showActions: function (data, op_map) {
        var self = this;
        if (!self.zipaiManager.isReplay) {
            self.nAction.setVisible(true);
            var show_options = [];
            var options = op_map;
            if (options[self.opType.OP_PASS].length > 0) {
                var guoBtn = this.listActBtn[3];
                guoBtn.paiArr = options[self.opType.OP_PASS];
                show_options.push(guoBtn);
            }
            if (options[self.opType.OP_CHI].length > 0) {
                var chiBtn = this.listActBtn[1];
                chiBtn.paiArr = options[self.opType.OP_CHI];
                show_options.push(chiBtn);
            }
            if (options[self.opType.OP_PENG].length > 0) {
                var pengBtn = this.listActBtn[2];
                pengBtn.paiArr = options[self.opType.OP_PENG];
                show_options.push(pengBtn);
            }
            if (options[self.opType.OP_HU].length > 0) {
                var huBtn = this.listActBtn[0];
                huBtn.paiArr = options[self.opType.OP_HU];
                show_options.push(huBtn);
            }
            for (var i = 0; i < show_options.length; i++) {
                var bt = show_options[i];
                bt.setPositionX(i * -200 - 100);
                bt.setVisible(true);
            }
        } else {
            var pos = self.zipaiManager.g_uid_to_pos[data['uid']];
            var replay_act = self.$('replay_act' + pos, self.PlayerInfoNode);
            if (!!replay_act) {
                replay_act.setVisible(true);
            }
        }
    },
    /**
     * 操作吃、碰、过、胡 按钮
     */
    onActionBtn: function (tag, target) {
        var self = this;
        if (self.isBeginTouch && self.movecard && self.positionBak) {
            if (self.zorderBak) self.movecard.setLocalZOrder(self.zorderBak);
            self.movecardTmp.setVisible(false);
            self.movecard.stopAllActions();
            self.movecard.setPosition(self.movecardTmp.getPosition());
            self.movecard.setOpacity(255);
            self.movecard.runAction(cc.sequence(
                cc.moveTo(0.1, self.positionBak),
                cc.callFunc(function (sender) {
                    self.movexy = null;
                    self.movetoxy = null;
                    self.movetoxybak = null;
                    self.movecard = null;
                    self.positionBak = null;
                    self.isMoving = false;
                    self.isBeginTouch = false;
                    sender = null;
                    // cc.log("----------onActionBtn  callFunc ");
                })
            ));
            return;
        }
        self.showNo = self.showNo || target.paiArr[0];
        var hubtn = self.$('bt1', this.nAction);
        if (tag == 2) {
            if (hubtn.isVisible() == true) {
                showMessageBox('当前可以胡牌，请问是否选择吃?', function () {
                    self.showChiSelect();
                });
            } else {
                self.showChiSelect();
            }
        } else if (tag == 3) {
            if (hubtn.isVisible() == true) {
                showMessageBox('当前可以胡牌，请问是否选择碰?', function () {
                    target.setVisible(false);
                    self.userAct(self.opType.OP_PENG, self.showNo, target);
                });
            } else {
                target.setVisible(false);
                self.userAct(self.opType.OP_PENG, self.showNo, target);
            }
        } else if (tag == 1) {
            self.userAct(self.opType.OP_HU, self.showNo, target);
        } else if (tag == 4) {
            //过得时候判断一下  有没有 胡的选项,有的话,提示
            if (hubtn.isVisible() == true) {
                showMessageBox('当前可以胡牌，请问是否选择过?', function () {
                    target.setVisible(false);
                    self.userAct(self.opType.OP_PASS, self.showNo, target);
                });
            } else {
                target.setVisible(false);
                self.userAct(self.opType.OP_PASS, self.showNo, target);
            }
        }
        Filter.remove(target);
    },
    /**
     * 发送吃碰杠胡
     * @param act
     * @param card
     * @param target
     * @param info
     * @param wangFan
     */
    userAct: function (act, card, target, info, wangFan) {
        var self = this;
        if (act == CHI) {
            act = self.opType.OP_CHI;
        }
        var paiArr = null;
        if (info) {
            var paiArr = [];
            for (var k = 0; k < info.length; k += 3) {
                var cards = [info[k], info[k + 1], info[k + 2]];
                paiArr.push(cards);
            }
        }
        if (act == self.opType.OP_HU &&
            (wangFan == self.opType.OP_WANGDIAO || wangFan == self.opType.OP_WANGCHUANG || wangFan == self.opType.OP_WANGZHA)){
            network.send(4003, {room_id: gameData.roomId, op: act, pai_id: card, paiArr: paiArr, wangFan:wangFan});
        }
        else {
            network.send(4003, {room_id: gameData.roomId, op: act, pai_id: card, paiArr: paiArr});
        }
        this.setTurnInOver();
    },
    addOutCard: function (isCombo) {
        if (this.lastShowCard != null && this.lastShowCard[3] != true) { //不是亮张
            if (isCombo != true) {
                var userPos = this.lastShowCard[1];
                var card = this.lastShowCard[0];
                var out = this.getOutNode(userPos);
                out.addCard(card, true, this.showCard);
            }
            this.lastShowCard = null;
        }
    },
    chuPai: function (card) {
        if (this.isFreeze(card)) {
            showAlert('漏牌不可以出！');
            return;
        }
        network.send(4002, {room_id: gameData.roomId, pai_id: card});
    },
    /**
     * 显示玩家信息
     * @param playerInfo
     */
    showPlayerInfoPanel: function (playerInfo) {
        var that = this;
        network.send(3018, {cmd: 'queryEmoji', uid: gameData.uid, room_id: gameData.roomId, showOption: 32767});
        showWindow('PlayerInfoInGameWindow', playerInfo, that);
    },
    /**
     * 设置重连回合信息
     */
    setRoundInfo: function () {
        var self = this;
        if (this.zipaiManager.isGameStart) {
            var RoundInfo = this.zipaiManager.RoundInfo;
            var turn_uid = RoundInfo['turn_uid'];
            var will_chu = RoundInfo['will_chu'];
            var cur_pai = RoundInfo['cur_pai'];
            var isMoPai = RoundInfo['isMoPai'];
            this.turnPos = self.zipaiManager.g_uid_to_pos[turn_uid];
            if (!will_chu) {
                self.showCardAtPos(cur_pai, this.turnPos, isMoPai);
            } else {
                this.secondsLeft = 15;
                this.setTurnOutArrow();
                if (this.turnPos == 0) {
                    this.isTurnOut = true;
                    this.isTurnIn = false;
                }
            }
            this.setUpGameStart();
        }
    },
    /**
     * 重连设置所有门子
     */
    setAllOpenCards: function () {
        for (var i = 0; i < this.playersNum; i++) {
            var openCards = this.getOpenNode(i);
            openCards.removeAllChildren();
            var uid = this.zipaiManager.g_pos_to_uid[i];
            var menzi = this.zipaiManager.g_uid_to_Menzi[uid];
            if (menzi) {
                for (var j = 0; j < menzi.length; j++) {
                    var cards = menzi[j]['pai_arr'].filter(function (value) {
                        return value > 0;
                    });
                    var typ = mCard.getJavaComboType(menzi[j]['type']);
                    openCards.addCards(cards, typ, false, menzi[j]['chiPaiId'],menzi[j]['huXi']);
                }
            }
        }
    },
    /**
     * 重连设置飘
     */
    setUpPiao: function () {

    },
    showOtherPai: function () {

    },
    /**
     * 小结算
     * 4008
     */
    showGameResult: function (isPlayMusic) {
        var that = this ;
        cc.sys.localStorage.setItem('Storage_handsCards', []);
        if(this.zipaiManager.gameOverData.xingPaiId){
            var game_zhang = that.$('RoomInfo.game_zhang');
            mEffect.fanxing(this.zipaiManager.gameOverData.xingPaiId,game_zhang,this.zipaiManager.gameOverData.hu_pai_id )
        }

        setTimeout(function () {
            that.setTurnInOver();
            that.showOtherPai();
            showWindow('ZiPai_JieSuanJavaWindow', this.zipaiManager.gameOverData, that);
            if (isPlayMusic) {
                var hu_type = this.zipaiManager.gameOverData['hu_type'];
                if (hu_type == 0 || hu_type == 3) {
                    playEffect('huangz');//胡不得
                } else {
                    that.playCPTPHEffect('hu', gameData.sex);
                }
                playEffect('getw');
            }
            that.panelResult.setVisible(!that.zipaiManager.isReplay);
        },600)

    },
    removeXing:function () {
        var game_zhang = this.$('RoomInfo.game_zhang');
        mEffect.fanxing(null,game_zhang,null,true)
    },
    /**
     * 听牌提示
     */
    setTishiList: function () {
        this.showTingPaiOnDesk(this.zipaiManager.TingCards);
        var tishiData = this.zipaiManager.TishiList; //
        if (tishiData) {
            var handList = this.getCardsNode(0).getChildren();
            for (var i = 0; i < handList.length; i++) {
                var card = handList[i];
                for (var paiId in tishiData) {
                    if (parseInt(paiId) == parseInt(card.data)) {
                        card.setTingMark(true);
                        break;
                    }
                }
            }
        }
    },
    /**
     * 点击听牌
     * @param paiId
     */
    touchTingPai: function (paiId) {
        var self = this;
        var tishiData = self.zipaiManager.TishiList;
        if (tishiData) {
            for (var card in tishiData) {
                if (parseInt(card) == parseInt(paiId)) {
                    self.showTingPaiOnDesk(tishiData[card], 'touchMove');
                    break;
                }
            }
        }
    },
    /**
     * 显示听牌数据
     * @param data
     * @param type
     */
    showTingPaiOnDesk: function (data, type) {
        var ting_pos = this.tingNode.getChildByName('ting_pos');
        var tingBg = this.tingNode.getChildByName('tingBg');
        this.tingNode.setVisible(false);
        ting_pos.removeAllChildren();
        if (!data || data.length == 0)
            return;
        var _len = data.length;
        for (var i = 0; i < _len; i++) {
            var tingCardInfo = data[i];
            var card = new Card();
            card.setData(tingCardInfo[0], null, 2);
            card.setAnchorPoint(0, 0.5);
            card.setPosition(i * 70, 1);
            ting_pos.addChild(card);

            var num = new ccui.Text();
            num.setString(tingCardInfo[1]);
            num.setFontSize(20);
            num.setAnchorPoint(cc.p(0, 0.5));
            num.setPosition(cc.p(48 + i * 70, 0));
            num.setColor(cc.color(255, 255, 255));
            ting_pos.addChild(num);
        }
        if (_len > 0) {
            tingBg.setContentSize(cc.size(70 + _len * 70, tingBg.getContentSize().height));
            this.tingNode.setVisible(true);
        } else {
            this.tingNode.setVisible(false);
        }
    },
    /**
     * 总结算
     */
    showRoomClean: function () {
        var self = this;
        var cardsNode = self.$('PlayerInfo.node_wang');
        if(cardsNode){
            cardsNode.removeAllChildren();
        }
        var RoomResultDate = this.zipaiManager.RoomResultDate;
        if (!RoomResultDate) return;
        if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) {
            showWindow('ZiPai_ZongJieSuanJavaWindow_qylds', RoomResultDate, RoomResultDate['club_id']);
        }
        else {
            showWindow('ZiPai_ZongJieSuanJavaWindow', RoomResultDate, RoomResultDate['club_id']);
        }
        this.zipaiManager.RoomResultDate = null;
    },
    /**
     * 死守
     * 5001
     * @param data
     */
    showSishouResult: function (data) {
        var self = this;
        var uid = data['uid'];
        var row = self.zipaiManager.g_uid_to_pos[uid];
        var sishou = self.$('info' + row + '.img_sishou', self.PlayerInfoNode);
        sishou.setVisible(true);
        self.nAction.runAction(
            cc.sequence(
                cc.delayTime(3),
                cc.callFunc(function () {
                    sishou.setVisible(false);
                })
            )
        );
    },
    /**
     * 检测是否是王钓、王闯、王炸中的任何一个
     */
    checkWangfan: function(data){
        var isWancfanType = false;
        var fanInfo = deepCopy(data);
        var wangfanArr = ['王钓', '王闯', '王炸', '王钓王', '王闯王', '王炸王'];
        var tmpFanInfo = {};
        var isFindWangfan = false;
        for (var i = 0; i < fanInfo.length; i++) {
            tmpFanInfo = fanInfo[i];
            if (isFindWangfan == true) break;
            for (var item in tmpFanInfo){
                if (wangfanArr.indexOf(item) > -1) {
                    isWancfanType = true;
                    isFindWangfan = true;
                    break;
                }
            }
        }

        return isWancfanType;
    }
    // onCCSLoadFinish:function () {
    //     var self = this;
    //     self.$ = create$(this.getRootNode());
    // },
});